﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;

namespace MotelServices
{
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class MotelServicesManager
    {
        #region Service function to get all address
        [WebGet(UriTemplate = "all_address")]
        public List<DiaChiDTO> getAllAddress()
        {
            return DiaChiBUS.LayDiaChi();
        }
        #endregion

        #region Service function to get all wards
        [WebGet(UriTemplate = "all_wards/{quan}")]
        public List<PhuongDTO> getAllWards(string quan)
        {
            return PhuongBUS.LayPhuong(Convert.ToInt32(quan));
        }
        #endregion

        #region Service function to get all districts
        [WebGet(UriTemplate = "all_districts")]
        public List<QuanDTO> getAllDistricts()
        {
            return QuanBUS.getDistrict();
        }
        #endregion

        #region Service function to get result search
        [WebGet(UriTemplate = "re_search/{maquan}/{maphuong}")]
        public List<DiaChiDTO> getresultsearch(string maquan,string maphuong)
        {
            return DiaChiBUS.searchaddress(Convert.ToInt32(maquan),Convert.ToInt32(maphuong));
        }
        #endregion

        #region Service function to insert address
        [WebInvoke(UriTemplate = "all_address",
          RequestFormat = WebMessageFormat.Json,
          ResponseFormat = WebMessageFormat.Json,
          Method = "POST")]
        public string insertAddress(DiaChiDTO requiredAddress)
        {
            string resultMessage = string.Empty;
            try
            {
                if (requiredAddress == null) { throw new Exception("Invalid Parameters !"); }

                // Save address info into database
                bool rs = DiaChiBUS.insertAddress(requiredAddress);
                resultMessage = " Insert successfully ! ";
            }
            catch (Exception ex)
            {
                resultMessage = string.Format("error : {0}", ex.Message);
            }
            return resultMessage;
        } 
        #endregion

        #region Serice function to delete address
        [WebInvoke(UriTemplate = "delete_address/{deletedAddress}",
          Method = "DELETE")]
        public string deleteAddress(string deletedAddress)
        {
            string resultMessage = string.Empty;
            try
            {
                int localDeletedAddress = int.Parse(deletedAddress);
                bool rs = DiaChiBUS.deleteAddress(localDeletedAddress);
                resultMessage = "Delete successfully !";
            }
            catch (Exception ex)
            {
                resultMessage = string.Format("error : {0}", ex.Message);
            }
            return resultMessage;
        }
        
        #endregion

        #region Service function to update student
        [WebInvoke(UriTemplate = "all_address",
                  RequestFormat = WebMessageFormat.Json,
                  ResponseFormat = WebMessageFormat.Json,
                  Method = "PUT")]
        public string updateAddress(DiaChiDTO requiredAddress)
        {
            string resultMessage = string.Empty;
            try
            {
                if (requiredAddress == null) { throw new Exception("Invalid Parameters !"); }

                bool rs = DiaChiBUS.updateAddress(requiredAddress);
                resultMessage = "Update successfully !";
            }
            catch (Exception ex)
            {
                resultMessage = string.Format("error : {0}", ex.Message);
            }
            return resultMessage;
        }
        #endregion
    }
}